﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace WindowsFormsApplication1.EnterGameRequests 
{
    public class EnterGameManager
    {
        private List<EnterGameRequest> enterGameRequests = new List<EnterGameRequest>();

        public bool SendEnterGameRequest(EnterGameRequest newRequest)
        {
            lock (enterGameRequests)
            {
                foreach (EnterGameRequest request in enterGameRequests)
                {
                    if (request.clientSocketData.UserID == newRequest.clientSocketData.UserID)
                        return false;
                }
                enterGameRequests.Add(newRequest);
            }

            StringWriter stringWriter = new StringWriter();
            XmlTextWriter xmldoc = new XmlTextWriter(stringWriter);

            xmldoc.WriteStartElement("addpendingclient");
            xmldoc.WriteAttributeString("userid", newRequest.clientSocketData.UserID.ToString());
            xmldoc.WriteAttributeString("characterid", newRequest.characterID.ToString());
            xmldoc.WriteAttributeString("uid", newRequest.uid);
            xmldoc.WriteAttributeString("mapid", newRequest.MapID.ToString());
            xmldoc.WriteAttributeString("instance", newRequest.Instance.ToString());
            xmldoc.WriteAttributeString("mapx", newRequest.MapX.ToString());
            xmldoc.WriteAttributeString("mapy", newRequest.MapY.ToString());
            xmldoc.WriteEndElement();

            xmldoc.Flush();
            xmldoc.Close();

            stringWriter.Flush();

            string xmlMessage = stringWriter.ToString();

            if (SocketConnectionsManager.sendToServer(newRequest.transferToServer, xmlMessage) == false)
            {
                lock (enterGameRequests)
                {
                    enterGameRequests.Remove(newRequest);
                    return false;
                }
            }
            return true;
        }
        public void ProcessEnterGameResult(int UserID, bool result)
        {
            lock (enterGameRequests)
            {
                foreach (EnterGameRequest enterGameRequest in enterGameRequests)
                {
                    if (enterGameRequest.clientSocketData.UserID == UserID)
                    {
                        enterGameRequests.Remove(enterGameRequest);
                        enterGameRequest.processResult(result);
                        break;
                    }
                }
            }
        }
        public void ProcessEnterGameResult(EnterGameRequest enterGameRequest, bool result)
        {
            lock (enterGameRequests)
            {
                enterGameRequests.Remove(enterGameRequest);
                enterGameRequest.processResult(result);
            }
        }
        public void ProcessTimeout(EnterGameRequest enterGameRequest)
        {
            lock (enterGameRequests)
            {
                enterGameRequests.Remove(enterGameRequest);
                enterGameRequest.processTimeout();
            }
        }
    }
}
